import caozuo from '@/api/caozuo'

const state = {
  all: []
}

const actions = {
  addToCart ({commit}, caozuos) {
    commit('add_to_cart', caozuos.id)
  },
  getAllCaozuos ({commit}) {
    caozuo.getCaozuos(caozuos => {
      commit('receive_caozuos', caozuos)
    })
  }
}

const mutations = {
  receive_caozuos (state, caozuos) {
    state.all = caozuos
  },
  add_to_cart (state, caozuosId) {
    state.all.find(cz => cz.id === caozuosId).inventory--
  }
}
const getters = {
  allCaozuos (state) {
    return state.all
  }
}
export default {
  state,
  actions,
  mutations,
  getters
}
